package org.jeecg.modules.flow.temp.controller;

import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.business.repertory.entity.WmsRepertory;
import org.jeecg.modules.business.repertory.service.IWmsRepertoryService;
import org.jeecg.modules.dock.config.JsonUtils;
import org.jeecg.modules.flow.temp.entity.UpdateHiddenStatusField;
import org.jeecg.modules.flow.temp.service.CreateTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    //
 * 创建人员:    zhaojun
 * 创建时间:    2023-12-02
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/temp/excel")
public class InputController {

    @Autowired
    private CreateTaskService createTaskService;

    @Autowired
    private IWmsRepertoryService wmsRepertoryService;

    /**
     * 通过excel导入生成入库任务及明细
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "物料信息管理-通过excel导入数据", notes = "物料信息管理-通过excel导入数据")
    @RequestMapping(value = "/importExcelTask", method = RequestMethod.POST)
    public Result<?> createTaskByExcel(HttpServletRequest request) {
        return createTaskService.importExcel(request);
    }


    @ApiOperation(value = "更新显隐字段", notes = "更新显隐字段")
    @PostMapping(value = "/updateHiddenStatusField")
    public Result<String> updateHiddenStatusField(@RequestBody UpdateHiddenStatusField updateHiddenStatusField) {
        Map map = createTaskService.updateHiddenStatusField(updateHiddenStatusField);
        return Result.ok(JsonUtils.toJson(map));
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "即时库存管理-通过excel导入数据", notes = "即时库存管理-通过excel导入数据")
    @RequestMapping(value = "/importExcelStock", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, String code) {
        return wmsRepertoryService.importExcel(request, code);
    }


    @ApiOperation(value = "针对于未完成的出库任务明细中的物料，匹配库存。如果无库存，则新建库存", notes = "针对于未完成的出库任务明细中的物料，匹配库存。如果无库存，则新建库存")
    @RequestMapping(value = "/useOutOrderMaterialAddStock", method = RequestMethod.GET)
    public Result useOutOrderMaterialAddStock(String taskId, String outHouse) {
        createTaskService.useOutOrderMaterialAddStock(taskId, outHouse);
        return Result.ok();
    }

    @ApiOperation(value = "更新仓库编码", notes = "更新仓库编码")
    @RequestMapping(value = "/changeWarehouse", method = RequestMethod.GET)
    public Result changeWarehouse(@RequestParam(value = "id") String id) {
        createTaskService.changeWarehouse(id);
        return Result.ok();
    }

    @ApiOperation(value = "测试更新库存的批次特性问题", notes = "更新仓库编码")
    @RequestMapping(value = "/updateRep", method = RequestMethod.GET)
    public Result updateRep(@RequestParam(value = "id") String id) {
        WmsRepertory wmsRepertory = wmsRepertoryService.getById(id);
        List<WmsRepertory> list = new ArrayList<>();
        list.add(wmsRepertory);
        wmsRepertoryService.updateRawFeature(list);
        wmsRepertoryService.updateBatchById(list);
        return Result.ok();
    }
}
